package com.example.lotterysystem.dao.mapper;

import com.example.lotterysystem.dao.dataObject.Encrypt;
import com.example.lotterysystem.dao.dataObject.UserDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    /**
     * 查询邮箱绑定的人数
     * @param email
     * @return count
     */
    @Select("select  count(*) from user where email=#{email}")
    int countByMail(@Param("email") String email);

    @Select("select  count(*) from user where phone_number=#{phoneNumber}")
    int countByPhone(@Param("phoneNumber") Encrypt number);

    @Insert("insert into user (user_name,email,phone_number,password,identity) " +
            "values (#{userName},#{email},#{phoneNumber},#{password},#{identity})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")//表示使用主键
    void insert(UserDO userDO);
    //当执行插入操作时，MyBatis 会使用数据库生成的主键，并将这个值赋给实体类的 id 属性，确保在对象插入后可以获取到最新的主键值。
    //keyProperty 表示要赋值的对象的名称

    @Select("select * from user where phone_number = #{phoneNumber}")
    UserDO getUserByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);

    @Select("select * from user where email = #{email}")
    UserDO getByMail(@Param("email") String email);

    @Select("<script> select * from user <if test=\"identity!=null\"> where identity=#{identity} </if> order by id desc </script>")
    List<UserDO> selectUserListByIdentity(@Param("identity") String identityString);

    //存在的人员id
    @Select("<script>" +
            " select id from user" +
            " where id in" +
            " <foreach item='item' collection = 'items' open='(' separator=',' close =')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<Long> selectExistByIds(@Param("items") List<Long> id);

    @Select("<script>" +
            " select * from user" +
            " where id in" +
            " <foreach item='item' collection = 'items' open='(' separator=',' close =')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("items") List<Long> ids);

    @Select("select * from user where id = #{userId}")
    UserDO selectById(@Param("userId") Long userId);

    @Delete("delete from user where id = #{id}")
    void deleteUser(@Param("id") Long userId);
}

